<?  require_once '../../system/init.php';
    require_once '../inc/header.php'; ?>

<div class="fullscreen">
    <div class="description">
        <div class="desc-left">
            <div class="title"></div>
            <div class="text"></div>
            <div class="date"></div>
        </div>
        <div class="desc-right">
            <div class="count"></div>
            <div class="author"></div>
        </div>
    </div>
</div>

<script>
$(document).ready(function() {
    
        var photos = [];      
        var tl = 4000;
        var eventId = 1;
        var photoCount = 0;
        var number = 1;

//        function preload(urls) {
//            $(urls).each(function() {
//                var url = this;
//                setTimeout(function () {
//                    (new Image()).src = url; 
//                }, 100);
//            });
//        }

    function preload(images) {
        if (typeof document.body == "undefined") return;
        
        try {
            var div = document.createElement("div");
            div.setAttribute("id", "preload");
            div.setAttribute("style", "display:none");
            var s = div.style;
            s.position = "absolute";
            s.top = s.left = 0;
            s.visibility = "hidden";
            document.body.appendChild(div);
            div.innerHTML = "<img src=\"/" + images.join("\" /><img src=\"/") + "\" />";
         }
         catch(e) {
             //Error. Do nothing.
        }
    }

    function getPhotos() {
            if(!eventId){
                eventId = 1;
            };
            $.get('get_photos.php?eventId=' + eventId , function (data) {
                if (data) {
                    photos = data.photos;
                    eventId = data.eventId;
                    photoCount = data.photoCount;
                    var loadCount = data.photoCount;
                    if(!data.photoCount){
                        loadCount = 1;
                    };
                    
                    $('#preload').remove();
                    
                    setTimeout(getPhotos, tl * loadCount);
                    
                    preload(data.urls);
                    number = 1;
                    showPhoto();
                }
            });
        }

        function showPhoto() {
            var photo = photos.pop();

            if (photo) {
//                $('.fullscreen').fadeOut('slow', function () {
                    $('.fullscreen .title').html(photo.name);
                    $('.fullscreen .text').html(photo.desc);
                    $('.fullscreen .count').html('Фото: ' + number++ + ' из ' + photoCount);
                    $('.fullscreen .date').html(photo.date);
                    $('.fullscreen .author').html(photo.author);
                    $('.fullscreen').css('background-image', 'url("/'+photo.url+'")');
//                });
            }
        }
        
        getPhotos();
                    
        setInterval(showPhoto, tl);
    
    
    
    
   });    
</script>

<script>
function reloadPage() {
    if (new Date().getHours() == 03 && new Date().getMinutes() == 00) {
          setTimeout(location.reload(), 60000);
    } 
}

$(document).ready(function(){
    setInterval("reloadPage();", 29000);
});
</script>